package com.xxxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.dao.*;
import com.xxxy.exception.MyException;
import com.xxxy.model.*;
import com.xxxy.response.StudentResponse;
import com.xxxy.service.TeachingClassStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sjh
 * @date 2022/2/2 13:30
 */
@Service
public class TeachingClassStudentServiceImpl extends ServiceImpl<TeachingClassStudentDao, TeachingClassStudent> implements TeachingClassStudentService {

    @Autowired
    StudentDao studentDao;

    @Autowired
    TeachingClassDao teachingClassDao;

    @Autowired
    TeachingClassStudentDao teachingClassStudentDao;

    @Autowired
    TeacherDao teacherDao;

    @Override
    public Map<String,List<Integer>> list(String account) {
        Map<String, List<Integer>> map = new HashMap<>();
        if(account.length() == 10){
            Student student = studentDao.selectById(account);
            if(student == null){
                throw new MyException("没有该账号信息");
            }
            //获取所属行政班级所上的课
            Integer classId = student.getAdministrativeClassId();
            LambdaQueryWrapper<TeachingClass> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TeachingClass::getAdministrativeClass, classId);
            List<TeachingClass> teachingClassList = teachingClassDao.selectList(lambdaQueryWrapper);
            List<Integer> list = new ArrayList<>();
            if(teachingClassList.size() != 0){
                for (TeachingClass teachingClass : teachingClassList){
                    list.add(teachingClass.getId());
                }
            }
            //获取其余课
            LambdaQueryWrapper<TeachingClassStudent> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(TeachingClassStudent::getStudentAccount, account);
            List<TeachingClassStudent> teachingClassStudentList = teachingClassStudentDao.selectList(lambdaQueryWrapper1);
            if(teachingClassStudentList.size() != 0){
                for (TeachingClassStudent teachingClassStudent : teachingClassStudentList){
                    list.add(teachingClassStudent.getTeachingClassId());
                }
            }
            map.put("Class", list);
        }
        else if(account.length() == 6){
            Teacher teacher = teacherDao.selectById(account);
            if(teacher == null){
                throw new MyException("没有该账号信息");
            }
            LambdaQueryWrapper<TeachingClass> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TeachingClass::getTeacherAccount, account);
            List<TeachingClass> teachingClassList = teachingClassDao.selectList(lambdaQueryWrapper);
            List<Integer> list = new ArrayList<>();
            if(teachingClassList.size() != 0){
                for (TeachingClass teachingClass : teachingClassList){
                    list.add(teachingClass.getId());
                }
            }
            map.put("Class", list);
        }
        return map;
    }

    @Override
    public Boolean deleteStudent(Integer teachingClassId, String studentAccount) {
        LambdaQueryWrapper<TeachingClassStudent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TeachingClassStudent::getTeachingClassId, teachingClassId);
        lambdaQueryWrapper.eq(TeachingClassStudent::getStudentAccount, studentAccount);
        teachingClassStudentDao.delete(lambdaQueryWrapper);
        return teachingClassStudentDao.delete(lambdaQueryWrapper) == 1;
    }

    @Override
    public CommonResultPage getStudentByTeachingClass(Integer page, Integer size,Integer teachingClassId) {
        PageHelper.startPage(page, size);
        List<StudentResponse> studentList = new ArrayList<>();
        TeachingClass teachingClass = teachingClassDao.selectById(teachingClassId);
        if(teachingClass.getAdministrative() ==0){
            studentList = teachingClassStudentDao.getStudentByTeachingClassId(teachingClassId);
        } else {
            Student student = new Student();
            student.setAdministrativeClassId(teachingClass.getAdministrativeClass());
            studentList = studentDao.list(student);
        }
        PageInfo<StudentResponse> pageInfo = new PageInfo<>(studentList);
        CommonResultPage result = new CommonResultPage();
        result.setTotal(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

}
